package android.handler;

import java.util.concurrent.Delayed;
import java.util.concurrent.TimeUnit;

/**
 * @author Ajie
 * @date 2021-03-15
 * @function
 */
public class Message implements Delayed {
    // 消息内容
    public Object obj;
    // 消息标志
    public int what;

    long when;

    // Handler对象
    public Handler target;


    @Override
    public long getDelay(TimeUnit unit) {
        return unit.convert(when - System.currentTimeMillis(),TimeUnit.MILLISECONDS);
    }

    @Override
    public int compareTo(Delayed o) {
        if (o instanceof Message) return (int)(when - ((Message)o).when);
        else return (int)(getDelay(TimeUnit.MILLISECONDS) - o.getDelay(TimeUnit.MILLISECONDS));
    }
}
